package com.ai_daily.aitiantian.util;

import android.os.Looper;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ThreadUtil {

    private static Executor sExecutor = Executors.newSingleThreadExecutor();
    private static Executor sMainExecutor = new MainThreadExecutor();

    /**
     * @param runnable
     */
    public static void runOnSubThread(Runnable runnable) {
        sExecutor.execute(runnable);
    }

    public static void runOnMainThread(Runnable runnable) {
        sMainExecutor.execute(runnable);
    }

    static class MainThreadExecutor implements Executor {

        /**
         * 主线程消息处理
         */
        private final WeakHandler handler = new WeakHandler(Looper.getMainLooper());

        @Override
        public void execute(Runnable command) {

            handler.post(command);

        }
    }
}
